home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / opengl / motif / oblur.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  7.6 KB  |  275 lines

  1. /*
  2.  * Copyright 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*----------------------------------------------------------------------------
  18.  *
  19.  * oblur.c : openGL (motif) example showing how to use accumulation buffers
  20.  *           for motion blurs
  21.  *
  22.  * Author : Yusuf Attarwala
  23.  *          SGI - Applications
  24.  * Date   : Mar 93
  25.  *
  26.  *    note : the main intent of this program is to acc buffer functionality,
  27.  *           hence the rendering is kept simple (wireframe) 
  28.  *
  29.  *    press  left   button for animation
  30.  *    press  middle button to turn OFF motion blur (default)
  31.  *    press  right  button to turn ON  motion blur 
  32.  *
  33.  *
  34.  *---------------------------------------------------------------------------*/
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37.  
  38. #include <Xm/Xm.h> 
  39. #include <Xm/Frame.h>               /* for frame widgets */
  40. #include <Xm/Form.h>                /* for frame widgets */
  41. #include <X11/keysym.h>             /* keyboard translations */
  42. #include <X11/StringDefs.h>
  43.  
  44. #include <GL/gl.h>                  /* gl includes */
  45. #include <GL/glu.h>                 /* utility library includes */
  46. #include <GL/GLwMDrawA.h>      /* include for the drawing area widget */
  47.  
  48. /* function declarations */
  49.  
  50. static int doBlur = 0;
  51. void 
  52.     createToplevel(void),
  53.     drawScene(void),
  54.     setMatrix(void),
  55.     animation(void),
  56.     exposeCB(Widget,XtPointer,XtPointer),
  57.     resizeCB(Widget,XtPointer,XtPointer),
  58.     initCB(Widget,XtPointer,XtPointer),
  59.     inputCB(Widget,XtPointer,XtPointer);
  60.  
  61.  
  62. /* global variables */
  63.             
  64. Display       *display;             /* current display */
  65. XtAppContext  appContext;           /* X application context */
  66. float         ax,ay,az;             /* angles for animation */
  67. GLUquadricObj *quadObj;             /* used in drawscene */
  68.  
  69. Widget        toplevel,       /* toplevel shell */
  70.               glw;            /* current widget */
  71.              
  72. GLXContext    glxc;           /* current glx context */
  73.  
  74. Arg args[20];
  75. int acnt;
  76.  
  77. void 
  78. main(int argc, char** argv)
  79. {
  80.     XtToolkitInitialize();
  81.     appContext = XtCreateApplicationContext();
  82.     display    = XtOpenDisplay(appContext, NULL, "Oblur","oblur",NULL,0,
  83.                               &argc,argv);
  84.     if (!display) {
  85.         printf("%s : Unable to open display\n",argv[0]);
  86.         exit(0);
  87.     }
  88.  
  89.     printf("\n---------------------------------------------\n");
  90.     printf("OpenGL motion blur using acc buffer example \n\n");
  91.     printf("Press:  left   button for animation\n\
  92.         middle button to turn OFF motion blur (default)\n\
  93.         right  button to turn ON  motion blur \n");
  94.  
  95.     quadObj = gluNewQuadric ();   /* this will be used in drawScene */
  96.     createToplevel();             /* create widget hierarchy */
  97.     XtAppMainLoop(appContext);    /* loop forever */
  98. }
  99.  
  100.  
  101. void
  102. createToplevel(void)
  103. {
  104.     Widget frame;
  105.  
  106.     acnt = 0;
  107.     XtSetArg(args[acnt],XmNminHeight, 300);acnt++;
  108.     XtSetArg(args[acnt],XmNminWidth,  300);acnt++;
  109.     XtSetArg(args[acnt],XmNminAspectX,  1);acnt++;
  110.     XtSetArg(args[acnt],XmNminAspectY,  1);acnt++;
  111.     XtSetArg(args[acnt],XmNmaxAspectX,  1);acnt++;
  112.     XtSetArg(args[acnt],XmNmaxAspectY,  1);acnt++;
  113.     toplevel  = XtAppCreateShell("openGL motionBlur","xtext",
  114.                                   applicationShellWidgetClass,
  115.                                   display,args,acnt);
  116.  
  117.     /* create a frame to hold glx widget */
  118.     frame   = XtVaCreateManagedWidget("frame", 
  119.                   xmFrameWidgetClass, toplevel, 
  120.                   NULL);
  121.  
  122.     /* create a double buffer widget, in rgb mode and manage it */
  123.     acnt = 0;
  124.     XtSetArg(args[acnt], GLwNrgba,               TRUE); acnt++;
  125.     XtSetArg(args[acnt], GLwNdoublebuffer,       TRUE); acnt++;
  126.     XtSetArg(args[acnt], GLwNallocateBackground, TRUE); acnt++;
  127.     XtSetArg(args[acnt], GLwNaccumRedSize,       1); acnt++;
  128.     XtSetArg(args[acnt], GLwNaccumGreenSize,     1); acnt++;
  129.     XtSetArg(args[acnt], GLwNaccumBlueSize,      1); acnt++;
  130.     glw = GLwCreateMDrawingArea(frame, "glw", args, acnt);
  131.     XtManageChild(glw);
  132.  
  133.     /* register callbacks */
  134.     XtAddCallback(glw, GLwNginitCallback,  initCB,   NULL);
  135.  
  136.     /* realize widget hierarchy */
  137.     XtRealizeWidget(toplevel);
  138.  
  139.     /* additional callbacks */
  140.     XtAddCallback(glw, GLwNexposeCallback, exposeCB, NULL);
  141.     XtAddCallback(glw, GLwNresizeCallback, resizeCB, NULL);
  142.     XtAddCallback(glw, GLwNinputCallback,  inputCB,  NULL);
  143.  
  144. }
  145.  
  146. void
  147. drawScene(void)
  148. {
  149.     glClearColor(0.0, 0.0, 0.0, 0.0);
  150.     glClear(GL_COLOR_BUFFER_BIT);
  151.  
  152.     glPushMatrix();
  153.     gluQuadricDrawStyle (quadObj, GLU_LINE);
  154.     glColor3f (1.0, 0.0, 0.0);
  155.     glRotatef (ax,1.0,0.0,0.0);
  156.     glRotatef (-ay,0.0, 1.0, 0.0);
  157.     gluCylinder (quadObj, 2.0,5.0,10.0,10,4);  /* draw a cone */
  158.     glPopMatrix();
  159.  
  160.     if (doBlur) {
  161.         glAccum(GL_MULT,0.33);
  162.         glAccum(GL_ACCUM,0.66);
  163.         glAccum(GL_RETURN,10.5);
  164.     }
  165.  
  166.     glFlush();
  167.     glXSwapBuffers(XtDisplay(glw), XtWindow(glw));
  168.  
  169. }
  170.  
  171. void
  172. setMatrix(void)
  173. {
  174.     glMatrixMode(GL_PROJECTION);
  175.     glLoadIdentity();
  176.     glOrtho(-15.0,15.0,-15.0,15.0,-15.0,15.0);
  177.     glMatrixMode(GL_MODELVIEW);
  178.     glLoadIdentity();
  179. }
  180.  
  181. void
  182. animation(void)
  183. {
  184.     register int i;
  185.     /* animate the cone */
  186.  
  187.     glClearAccum(0.0,0.0,0.0,0.0);
  188.     glClear(GL_ACCUM_BUFFER_BIT);
  189.     for (i=0;i<40;i++) {
  190.         ax += 15.0;
  191.         ay -= 12.0;
  192.         az += 15.0;
  193.         if (ax >= 360)  ax = 0.0;
  194.         if (ay <= -360) ay = 0.0;
  195.         if (az >= 360)  az = 0.0;
  196.         drawScene();
  197.     }
  198. }
  199.  
  200.  
  201. void 
  202. inputCB(Widget w, XtPointer client_data, XtPointer call)
  203. {
  204.     char buffer[1];
  205.     KeySym keysym;
  206.     GLwDrawingAreaCallbackStruct *call_data;
  207.  
  208.     call_data = (GLwDrawingAreaCallbackStruct *) call;
  209.  
  210.     switch(call_data->event->type) {
  211.     case ButtonPress:
  212.         switch (call_data->event->xbutton.button) {
  213.         case Button1:
  214.         animation();
  215.             break;
  216.         case Button2 :
  217.         doBlur = 0;
  218.         fprintf(stdout,"Motion Blur DISABLED \n\n");
  219.         fflush(stdout);
  220.         drawScene();
  221.             break;
  222.         case Button3 :
  223.         doBlur = 1;
  224.         fprintf(stdout,"Motion Blur ENABLED \n\n");
  225.         fflush(stdout);
  226.         drawScene();
  227.             break;
  228.         }
  229.         break;
  230.     default:
  231.         break;
  232.     }
  233. }
  234.  
  235. void 
  236. resizeCB(Widget w, XtPointer client_data, XtPointer call)
  237. {
  238.     GLwDrawingAreaCallbackStruct *call_data;
  239.     call_data = (GLwDrawingAreaCallbackStruct *) call;
  240.  
  241.  
  242.     GLwDrawingAreaMakeCurrent(w, glxc);
  243.     glViewport(0, 0, call_data->width, call_data->height);
  244.     setMatrix();
  245.     drawScene();
  246. }
  247.  
  248. void 
  249. exposeCB(Widget w, XtPointer client_data, XtPointer call)
  250. {
  251.     GLwDrawingAreaCallbackStruct *call_data;
  252.     call_data = (GLwDrawingAreaCallbackStruct *) call;
  253.  
  254.  
  255.     GLwDrawingAreaMakeCurrent(w, glxc);
  256.     glViewport(0, 0, call_data->width, call_data->height);
  257.     drawScene();
  258. }
  259.  
  260. void
  261. initCB(Widget w, XtPointer client_data, XtPointer call)
  262. {
  263.     XVisualInfo *vi;
  264.  
  265.  
  266.     XtSetArg(args[0], GLwNvisualInfo, &vi);
  267.     XtGetValues(w, args, 1);
  268.  
  269.     glxc = glXCreateContext(XtDisplay(w), vi, 0, GL_TRUE);
  270.  
  271.     ax = 10.0;
  272.     ay = -10.0;
  273.     az = 0.0;
  274. }
  275.